package com.corex.vinayak;

import java.util.List;

import com.corex.data.AllEnrolledClassData;
import com.corex.utils.Constants;
import com.corex.vo.AllEnrolledClassDetail;
import com.corex.vo.ClassDetails;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ClassInfoAndSelectionAction extends Activity {
	TextView classInfoName;
	ImageView classIcon;
	ListView menuListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.class_info_selection);
		classInfoName = (TextView) findViewById(R.id.classinfo_name);
		classIcon = (ImageView) findViewById(R.id.classinfo_icon);
		menuListView = (ListView) findViewById(R.id.classinfo_menu_btn);

		String classCode = (String) getIntent().getExtras().get(
				Constants.LOGINSUCESS_SELECTED_CLASS_CODE);
		AllEnrolledClassDetail allEnrolledClassDetail = AllEnrolledClassData
				.getAllEnrolledClassDetail();
		List<ClassDetails> classDetails = allEnrolledClassDetail
				.getClassDetails();

		for (int i = 0; i < classDetails.size(); i++) {
			if (((ClassDetails) classDetails.get(i)).getClassCode().equals(
					classCode)) {
				classInfoName.setText(((ClassDetails) classDetails.get(i))
						.getClassName());
				classIcon.setImageResource(((ClassDetails) classDetails.get(i))
						.getIcon());
				break;
			}
		}

		// Defined Array values to show in ListView
		final String[] values = Constants.values;
		// Define a new Adapter
		// First parameter - Context
		// Second parameter - Layout for the row
		// Third parameter - ID of the TextView to which the data is written
		// Forth - the Array of data

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);

		// Assign adapter to ListView
		menuListView.setAdapter(adapter);

		// ListView Item Click Listener
		menuListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// ListView Clicked item index
				int itemPosition = position;

				// ListView Clicked item value
				String itemValue = (String) menuListView
						.getItemAtPosition(position);

				// Show Alert
				Toast.makeText(
						getApplicationContext(),
						"Position :" + itemPosition + "  ListItem : "
								+ itemValue, Toast.LENGTH_LONG).show();

				Intent intent = new Intent(ClassInfoAndSelectionAction.this, SubjectActivity.class);
				if(itemValue.equalsIgnoreCase(values[0])){
					intent.putExtra(Constants.SELECTION, values[0]);
				}else if(itemValue.equalsIgnoreCase(values[1])){
					intent.putExtra(Constants.SELECTION, values[1]);
				}else if(itemValue.equalsIgnoreCase(values[2])){
					intent.putExtra(Constants.SELECTION, values[2]);
				}
				startActivity(intent);
			}

		});
	}

}
